/**
 * 返回文字描述的日期
 *
 * @param date
 * @return
 */
export function getTimeFormatText(date: Date): string {
  const minute = 60 * 1000; // 1分钟
  const hour = 60 * minute; // 1小时
  const day = 24 * hour; // 1天
  const month = 31 * day; // 月
  const year = 12 * month; // 年
  const diff = new Date().getTime() - date.getTime();
  let r = 0;
  if (diff > year) {
    r = Number.parseInt((diff / year).toString());
    return r + "年前";
  }
  if (diff > month) {
    r = Number.parseInt((diff / month).toString());
    return r + "个月前";
  }
  if (diff > day) {
    r = Number.parseInt((diff / day).toString());
    return r + "天前";
  }
  if (diff > hour) {
    r = Number.parseInt((diff / hour).toString());
    return r + "个小时前";
  }
  if (diff > minute) {
    r = Number.parseInt((diff / minute).toString());
    return r + "分钟前";
  }
  return "刚刚";
}

/**
 * 返回格式化后的日期
 *
 * @param (date,parttern)
 * @param pattern "yyyy.dd.mm"
 *
 * @return "2018.01.01"
 */

export function dateFormat(millionsTime, pattern) {
  const d = new Date();
  d.setTime(millionsTime);
  const date = {
    "M+": d.getMonth() + 1,
    "d+": d.getDate(),
    "h+": d.getHours(),
    "m+": d.getMinutes(),
    "s+": d.getSeconds(),
    "q+": Math.floor((d.getMonth() + 3) / 3),
    "S+": d.getMilliseconds(),
  };
  if (/(y+)/i.test(pattern)) {
    pattern = pattern.replace(
      RegExp.$1,
      (d.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (const k in date) {
    if (new RegExp("(" + k + ")").test(pattern)) {
      pattern = pattern.replace(
        RegExp.$1,
        RegExp.$1.length == 1
          ? date[k]
          : ("00" + date[k]).substr(("" + date[k]).length)
      );
    }
  }
  return pattern;
}


console.log(getTimeFromDate(new Date().getTime()))
/**
 * 返回格式化后的日期
 *
 * @param xxxx年xx月xx日 xx：xx
 * 
 */

export function dateYearToDate(createTime) {
  const date = new Date(createTime)
  const Y = date.getFullYear() + '年'
  const M = date.getMonth() + 1 + '月'
  const D = date.getDate() + '日'
  const h = date.getHours() + ':'
  const m = date.getMinutes()
  return {
    yearToDate: Y + M + D,
    time: h + m
  }
}


export function getTimeFromDate(time: number) {
  const data = new Date(time)
  const minute = (data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes()); // 分钟
  const hour = (data.getHours() < 10 ? '0' + data.getHours() : data.getHours()) + ':'; // 小时
  const day = (data.getDate() < 10 ? '0' + data.getDate() : data.getDate()) + '   ' // 天
  const month = (data.getMonth() + 1 < 10 ? '0' + (data.getMonth() + 1) : (data.getMonth() + 1)) + '-' // 月
  const year = data.getFullYear() + '-'; // 年

  return year + month + day + hour + minute
}
